﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;
using System.Data;

namespace CRM.Web.FormManage
{
    public partial class FormModify : Common.PageBase
    {
        T_FormInfo_BLL bll = new T_FormInfo_BLL();
        T_FormFieldInfo_BLL fBll = new T_FormFieldInfo_BLL();


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                pageDataInit();
            }
        }

        private void pageDataInit()
        {
            string _formid = String.IsNullOrEmpty(Request.QueryString["FORMID"]) ? "" : Request.QueryString["FORMID"].Trim();

            if (!String.IsNullOrEmpty(_formid))
            {
                T_FormInfo_Entity entity = bll.GetEntity("FormID", _formid);

                PageHelper.SetControlValue<T_FormInfo_Entity>(this.Page, entity);
                btnCreate.Visible = true;

            }
            else
            {
                btnCreate.Visible = false;
            }
            txtCreatePeople.Enabled = false;
            txtCreateTime.Enabled = false;
            txtModifyPeople.Enabled = false;
            txtModifyTime.Enabled = false;
            txtFormID.Enabled = false;


        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            string _formid = String.IsNullOrEmpty(Request.QueryString["FORMID"]) ? "" : Request.QueryString["FORMID"].Trim();
            T_FormInfo_Entity entity = bll.GetEntity("FormID", _formid);
            T_FormFieldInfo_Entity fEntity;
            string fileName = string.Empty;
            string fileDescription = string.Empty;

            if (entity != null)
            {
                DataTable dt = bll.GetFieldList("", entity.TableName);
                for (int i = 0; i< dt.Rows.Count;  i++)
                {

                    fileDescription = dt.Rows[i]["COMMENTS"].ToString();
                    fileName = dt.Rows[i]["COLUMN_NAME"].ToString();
                    if (fBll.GetCount(string.Format(" and FormID = '{0}' and FieldName = '{1}' ", _formid, fileName)) == 0)
                    {
                        fEntity = new T_FormFieldInfo_Entity();
                        fEntity.ColSpan = 1;
                        fEntity.CreatePeople = _userLoginInfo.UserEntity.USERNAME;
                        fEntity.CreatePeopleId = _userLoginInfo.UserEntity.USERID.ToString();
                        fEntity.CreateTime = DateTime.Now;
                        fEntity.CssClass = "";
                        fEntity.DataSourceInfo = "";
                        fEntity.DataSourceType = (int)DP.Web.UI.Controls.DataSourceType.String;
                        fEntity.DefaultValue = "";
                        fEntity.ExclusiveRow = 0;
                        fEntity.FieldDescription = fileDescription;
                        fEntity.FieldName = fileName;
                        fEntity.FieldType = (int)DP.Web.UI.Controls.WebFormFieldType.TextBox;
                        fEntity.FormFieldID = Guid.NewGuid().ToString();
                        fEntity.FormID = _formid;
                        fEntity.Fromat = "";
                        fEntity.IsBlankRow = 1;
                        fEntity.IsEnabled = 1;
                        fEntity.IsPostBack = 0;
                        fEntity.MaxLenght = 0;
                        fEntity.ModifyPeople = _userLoginInfo.UserEntity.USERNAME;
                        fEntity.ModifyPeopleId = _userLoginInfo.UserEntity.USERID.ToString();
                        fEntity.ModifyTime = DateTime.Now;
                        fEntity.RelationFields = "";
                        fEntity.Remark = "";
                        fEntity.SortNumber = (i + 1);
                        fEntity.Width = 80;
                        fBll.Insert(fEntity);
                    }                    
                }

            }
            

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string _formid = String.IsNullOrEmpty(Request.QueryString["FORMID"]) ? "" : Request.QueryString["FORMID"].Trim();

            T_FormInfo_Entity entity = bll.GetEntity("FormID", _formid);

            if (entity != null)
            {
                PageHelper.GetControlValue<T_FormInfo_Entity>(this.Page, ref entity);
                entity.ModifyPeople = _userLoginInfo.UserEntity.USERNAME;
                entity.ModifyPeopleId = _userLoginInfo.UserEntity.USERID.ToString();
                entity.ModifyTime = DateTime.Now;

                try
                {
                    long rel = bll.Update(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "修改成功！", UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this), "formlist.aspx"));
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "修改失败！");
                    }
                }
                catch (Exception ex)
                {

                    PageHelper.ShowMessage(this.Page, "修改失败！" + ex.Message);
                }
            }
            else
            {
                entity = new T_FormInfo_Entity();

                PageHelper.GetControlValue<T_FormInfo_Entity>(this.Page, ref entity);
                entity.FormID = Guid.NewGuid().ToString();
                entity.CreatePeople = _userLoginInfo.UserEntity.USERNAME;
                entity.CreatePeopleId = _userLoginInfo.UserEntity.USERID.ToString();
                entity.CreateTime = DateTime.Now;
                entity.ModifyPeople = _userLoginInfo.UserEntity.USERNAME;
                entity.ModifyPeopleId = _userLoginInfo.UserEntity.USERID.ToString();
                entity.ModifyTime = DateTime.Now;


                try
                {
                    long rel = bll.Insert(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "添加成功！", "formlist.aspx");
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "添加失改！");
                    }

                }
                catch (Exception ex)
                {

                    PageHelper.ShowMessage(this.Page, "添加失改！" + ex.Message);
                }
            }
        }
    }
}
